
import copy

from coffeetalk.protocol import ioelement, ioml

class Request(object):
    """Base class for all Request objects. You must inherit from this class. 
    
        All Requests have five properties in common:
            requestid - a string representing the id of the request from the client
            path - path being requested.
            on_success - function to be called when the request is completed successfully
            on_error - function to be called the the request results in an error
            **kwargs - additional keyword arguments that will be passed to handlers
    """
    def __init__(self,requestid='0',path='/',on_success=None,on_error=None,**kwargs):
        self.requestid = requestid
        if self.requestid == '0':
            self.requestid = str(id(self))
        self.path = path
        self.on_success = on_success
        self.on_error = on_error
        self.__kwargs = copy.deepcopy(kwargs)
        
    def _getkwargs(self): return self.__kwargs
    def _setkwargs(self,value): self.__kwargs = copy.deepcopy(value)
    kwargs = property(_getkwargs,_setkwargs) 
    
    def __str__(self):
        return str(self.to_ioml())
    
    def to_ioml(self):
        return ioml.IOML(headelement=ioelement.IOElement('Request',{'type':'Request','requestid':self.requestid,'path':self.path}))
       
class Login(Request):
    """username,password,userclass,requestid='0',on_success=None,on_error=None,**kwargs"""
    def __init__(self,username,password,group,userclass='Users',requestid='0',clienttype='Python',on_success=None,on_error=None,**kwargs):
        Request.__init__(self,requestid=requestid,path='[Login]',on_success=on_success,on_error=on_error,**kwargs) 
        self.username = username
        self.password = password
        self.group = group
        self.userclass = userclass
        self.clienttype = clienttype
    
    def to_ioml(self):
        return ioml.IOML(headelement=ioelement.IOElement('Request',{'type':'Login','requestid':self.requestid,'path':self.path,'clienttype':self.clienttype,'username':self.username,'password':self.password,'group':self.group}))
    
class Subscribe(Request):
    """requestid='0',path='/',on_add=None,on_delete=None,threaded=False,rawelement=False,on_success=None,on_error=None,**kwargs"""
    def __init__(self,requestid='0',path='/',on_add=None,on_delete=None,anchor=False,threaded=False,rawelement=False,on_success=None,on_error=None,**kwargs):
        Request.__init__(self,requestid=requestid,path=path,on_success=on_success,on_error=on_error,**kwargs)
        self.on_add = on_add
        self.on_delete = on_delete
        self.anchor = anchor
        self.threaded = threaded
        self.rawelement = rawelement
    
    def to_ioml(self):
        return ioml.IOML(headelement=ioelement.IOElement('Request',{'type':'Subscribe','requestid':self.requestid,'path':self.path,'anchor':str(self.anchor)}))

class Unsubscribe(Request):
    """requestid='0',path='/',on_add=None,on_delete=None,on_success=None,on_error=None,**kwargs"""
    def __init__(self,requestid='0',path='/',anchor=False,on_add=None,on_delete=None,on_success=None,on_error=None,**kwargs):
        Request.__init__(self,requestid=requestid,path=path,on_success=on_success,on_error=on_error,**kwargs)
        self.on_add = on_add
        self.on_delete = on_delete
        self.anchor = anchor
        
    def to_ioml(self):
        return ioml.IOML(headelement=ioelement.IOElement('Request',{'type':'Unsubscribe','requestid':self.requestid,'path':self.path,'anchor':str(self.anchor)}))
             
class Get(Request):
    """requestid='0',path='/',on_get=None,threaded=False,rawelement=False,on_success=None,on_error=None,**kwargs"""
    def __init__(self,requestid='0',path='/',on_get=None,on_partial=None,threaded=False,rawelement=False,on_success=None,on_error=None,**kwargs):
        Request.__init__(self,requestid=requestid,path=path,on_success=on_success,on_error=on_error,**kwargs)
        self.on_get = on_get
        self.on_partial = on_partial
        self.threaded = threaded
        self.rawelement = rawelement
    
    def to_ioml(self):
        return ioml.IOML(headelement=ioelement.IOElement('Request',{'type':'Get','requestid':self.requestid,'path':self.path}))
    
class Add(Request):
    """requestid='0',path='/',iobject=None,removeon_disconnect=False,on_success=None,on_error=None,**kwargs"""
    def __init__(self,requestid='0',path='/',iobject=None,threaded=False,rawelement=False,on_add=None,on_success=None,on_error=None,**kwargs):
        Request.__init__(self,requestid=requestid,path=path,on_success=on_success,on_error=on_error,**kwargs)
        self.threaded = threaded
        self.rawelement = rawelement
        self.on_add = on_add
        self.iobject = iobject
        
    def to_ioml(self):
        headelement = ioelement.IOElement('Request',{'type':'Add','requestid':self.requestid,'path':self.path})
        bodyelement = self.iobject._to_element()
        return ioml.IOML(headelement=headelement,bodyelement=bodyelement)
    
class Delete(Request):
    """requestid='0',path='/',key='',on_success=None,on_error=None,**kwargs"""
    def __init__(self,requestid='0',path='/',key='',on_success=None,on_error=None,**kwargs):
        Request.__init__(self,requestid=requestid,path=path,on_success=on_success,on_error=on_error,**kwargs)
        self.key = key
        
    def to_ioml(self):
        return ioml.IOML(headelement=ioelement.IOElement('Request',{'type':'Delete','requestid':self.requestid,'path':self.path,'key':self.key}))
    
class AddMonitor(Request):
    """requestid='0',path='/',monitortype='Add',on_add=None,on_delete=None,on_success=None,on_error=None,**kwargs"""
    def __init__(self,requestid='0',path='/',monitortype='Add',threaded=False, rawelement=False,anchor=False,on_monitor=None,on_success=None,on_error=None,**kwargs):
        Request.__init__(self,requestid=requestid,path=path,on_success=on_success,on_error=on_error,**kwargs)
        self.monitortype = monitortype
        self.on_monitor = on_monitor
        self.threaded = threaded
        self.rawelement = rawelement
        self.anchor = anchor
        self.on_add = self.on_monitor
        
        #if not monitortype in monitors.MONITOR_TYPES:
        #    raise AttributeError, 'Monitor type must be one of the following: Get, Add, Delete, Subscribe, or Unsubscribe'
    def _setonmonitor(self,value): self.on_add = value
    def _getonmonitor(self): return self.on_add
    on_monitor = property(_getonmonitor,_setonmonitor)   
            
    def to_ioml(self):
        return ioml.IOML(headelement=ioelement.IOElement('Request',{'type':'AddMonitor','monitortype':str(self.monitortype),'requestid':self.requestid,'path':self.path}))
    
class DeleteMonitor(Request):
    """requestid='0',path='/',monitortype='Add',on_success=None,on_error=None,**kwargs"""
    def __init__(self,requestid='0',path='/',monitortype='Add',on_success=None,on_error=None,**kwargs):
        Request.__init__(self,requestid=requestid,path=path,on_success=on_success,on_error=on_error,**kwargs)
        self.monitortype = monitortype
        
    def to_ioml(self):
        return ioml.IOML(headelement=ioelement.IOElement('Request',{'type':'DeleteMonitor','monitortype':str(self.monitortype),'requestid':self.requestid,'path':self.path}))

class AddAccess(Request):
    """requestid='0',path='/',username='',group='',permissions='---------',on_success=None,on_error=None,**kwargs"""
    def __init__(self,requestid='0',path='/',username='',group='',permissions='---------',on_success=None,on_error=None,**kwargs):
        Request.__init__(self,requestid=requestid,path=path,on_success=on_success,on_error=on_error,**kwargs)
        self.username = username
        self.group = group
        self.permissions = permissions
        
    def to_ioml(self):
        return ioml.IOML(headelement=ioelement.IOElement('Request',{'type':'AddAccess','username':self.username,'group':self.group,'permissions':self.permissions,'requestid':self.requestid,'path':self.path}))

class RemoveAccess(Request):
    """requestid='0',path='/',accesskey='',on_success=None,on_error=None,**kwargs"""
    def __init__(self,requestid='0',path='/',accesskey='',on_success=None,on_error=None,**kwargs):
        Request.__init__(self,requestid=requestid,path=path,on_success=on_success,on_error=on_error,**kwargs)
        self.accesskey = accesskey
    
    def to_ioml(self):
        return ioml.IOML(headelement=ioelement.IOElement('Request',{'type':'RemoveAccess','accesskey':self.accesskey,'requestid':self.requestid,'path':self.path}))

class AddExtension(Request):
    """requestid='0',path='/',on_success=None,on_error=None,**kwargs):"""
    def __init__(self,requestid='0',path='/',on_success=None,on_error=None,**kwargs):
        Request.__init__(self,requestid=requestid,path=path,on_success=on_success,on_error=on_error,**kwargs)
        
    def to_ioml(self):
        return ioml.IOML(headelement=ioelement.IOElement('Request',{'type':'AddExtension','path':self.path,'requestid':self.requestid}))
    
class RemoveExtension(Request):
    """requestid='0',path='/',on_success=None,on_error=None,**kwargs):"""
    def __init__(self,requestid='0',path='/',on_success=None,on_error=None,**kwargs):
        Request.__init__(self,requestid=requestid,path=path,on_success=on_success,on_error=on_error,**kwargs)
        
    def to_ioml(self):
        return ioml.IOML(headelement=ioelement.IOElement('Request',{'type':'RemoveExtension','path':self.path,'requestid':self.requestid}))

class AddAnchor(Request):
    """requestid='0',path='/',on_success=None,on_error=None,**kwargs):"""
    def __init__(self,requestid='0',path='/',on_success=None,on_error=None,**kwargs):
        Request.__init__(self,requestid=requestid,path=path,on_success=on_success,on_error=on_error,**kwargs)
        
    def to_ioml(self):
        return ioml.IOML(headelement=ioelement.IOElement('Request',{'type':'AddAnchor','path':self.path,'requestid':self.requestid}))

class RemoveAnchor(Request):
    """requestid='0',path='/',on_success=None,on_error=None,**kwargs):"""
    def __init__(self,requestid='0',path='/',on_success=None,on_error=None,**kwargs):
        Request.__init__(self,requestid=requestid,path=path,on_success=on_success,on_error=on_error,**kwargs)
        
    def to_ioml(self):
        return ioml.IOML(headelement=ioelement.IOElement('Request',{'type':'RemoveAnchor','path':self.path,'requestid':self.requestid}))
    
      
class GetInternal(Request):
    """requestid='0',path='/',internaltype='[Access]',on_success=None,on_error=None,on_get=None,**kwargs"""
    def __init__(self,requestid='0',path='/',internaltype='[Access]',on_success=None,on_error=None,on_get=None,**kwargs):
        Request.__init__(self, requestid=requestid, path=path, on_success=on_success, on_error=on_error,**kwargs)
        self.internaltype = internaltype
        self.on_get = on_get
        
    def to_ioml(self):
        return ioml.IOML(headelement=ioelement.IOElement('Request',{'type':'GetInternal','internaltype':self.internaltype,'requestid':self.requestid,'path':self.path}))
    